package org.iteam.bank.mina.utils;

import java.io.UnsupportedEncodingException;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HexUtils {
	private static Logger logger = LoggerFactory.getLogger(HexUtils.class);

	/**
	 * 编码
	 * 
	 * @param data
	 * @return
	 */
	public static String encodeHexString(String data) {
		try {
			return Hex.encodeHexString(data.getBytes("UTF-8"));
		} catch (UnsupportedEncodingException e) {
			logger.error(e.getMessage(), e);
		}
		return data;
	}

	/**
	 * 编码
	 * 
	 * @param data
	 * @return
	 */
	public static String encodeHexString(byte[] data) {
		return Hex.encodeHexString(data);
	}

	/**
	 * 解码
	 * 
	 * @param data
	 * @return
	 */
	public static byte[] decodeHexByte(String data) {
		try {
			return Hex.decodeHex(data.toCharArray());
		} catch (DecoderException e) {
			logger.error(e.getMessage(), e);
		}
		return null;
	}

	/**
	 * 解码
	 * 
	 * @param data
	 * @return
	 */
	public static String decodeHex(String data) {
		try {
			return new String(Hex.decodeHex(data.toCharArray()));
		} catch (DecoderException e) {
			logger.error(e.getMessage(), e);
		}
		return data;
	}
}
